<template>
    <!-- 每天最多发布3条 每条无线字数 -->
    <div class="wraps">
        <div :class="['homewarp', isEditing ? 'ishide' : '']">
            <div class="search-box">
                <search v-model="searchKey"></search>
            </div>
            <div class="content-box">
                <div class="note-box" v-for="note in showDataSource" :key="note.id" @click="onClickEditor(note)">
                    <div v-html="note.content"></div>
                    <span @click.stop="onDeleteNote(note.id)">
                        <v-icon class="delete-btn" name="icon_delete" :size="30"></v-icon>
                    </span>
                </div>
            </div>
            <div class="addbtns" @click="onClickEditor">
                <v-icon style="color:#fff" name="icon_add" :size="40"></v-icon>
            </div>
        </div>
        <div :class="['editors', isEditing ? 'isshow' : '']">
            <div class="editors-top">
                <div class="back" @click="onClickEditor">
                    <v-icon name="icon_back" :size="20"></v-icon>
                </div>
                <div @click="onSave" class="saves">{{ $t('common.btn.save') }}</div>
            </div>
            <div class="toolbars">
                <toolbar></toolbar>
            </div>
            <div class="editors-warp">
                <div class="editwarp" @input="onchangetext" ref="noteHtmlRef" contenteditable="true" v-html="currentNote.content"
                    :data-hodle="$t('workTodos.limit', [maxlen])">
                </div>
            </div>
            <div :class="[iserror ? 'errors' : 'default']">
                {{ iserror ? $t('workTodos.limit.tip') : `${strlen}/${maxlen}`}}
            </div>
        </div>
    </div>
</template>
<script setup lang="ts">
import search from './search.vue'
import toolbar from '@/views/components/toolbar.vue';
import { message } from 'ant-design-vue';
const { t } = useI18n();

// 便签最大字数
const maxlen = 200;
// 输入的长度
const strlen = ref<any>(0)
const currentNote = ref<any>('')
// 是否是编辑状态
const isEditing = ref<boolean>(false)
// 便签数据
const dataSource = ref<any>([])
const noteHtmlRef = ref<any>(null)
const iserror = ref<any>(false)

// 搜索
const searchKey = ref<any>('');
const showDataSource = computed(() => {
    if(searchKey.value){
        return dataSource.value.filter((item: any) => item.content.includes(searchKey.value))
    }
    else{
        return dataSource.value
    }
})

// 获取便签
const getNotes = async () => {
    try{
        const res = await window.electron.onGetDatas(2);
        dataSource.value = JSON.parse(res);
    }catch(err){
        message.error(err.toString())
    }
}
// 删除便签
const onDeleteNote = async (id: any) => {
    try{
        dataSource.value = dataSource.value.filter((note: any) => note.id !== id)
        await window.electron.onSetDatas(JSON.stringify(dataSource.value), 2);
    }catch(err){
        message.error(err.toString())
    }
}
// 增加便签
const addNotes = async (note: any) => {
    try{
        // 编辑
        if(currentNote.value.id){
            dataSource.value.map((item: any) => {
                if(item.id === currentNote.value.id){
                    item.content = note;
                }
            })
        }
        // 新增
        else{
            dataSource.value.push({
                id: (new Date()).getTime(),
                content: note
            });
        }
        await window.electron.onSetDatas(JSON.stringify(dataSource.value), 2);
    }catch(err){
        message.error(err.toString())
    }
}

const onchangetext = (e: any) => {
    let str = e.target.innerText.replace(/\n/g, '').replace(/\s/g, '');
    iserror.value = str.length > maxlen;
    strlen.value = str.length;
}
// 保存
const onSave = async () => {
    const html = noteHtmlRef.value.innerHTML;
    let str = html.replace(/\<(\/)?.\>/g, '').replace(/\n/g, '').replace(/\s/g, '');
    //需要储存的数据  
    if(str.length > maxlen) {
        message.error(t("workTodos.limit.tip"));
    }
    else if(str.length === 0){
        message.error(t("workTodos.limit.empty"));
    }
    else {
        await addNotes(html);
        isEditing.value = false;
        currentNote.value = {};
        noteHtmlRef.value.innerHTML = '';
    }
}
// 新增/编辑/取消
const onClickEditor = (note: any) => {
    if(note){
        currentNote.value = note;
    }
    isEditing.value = !isEditing.value;
    if(!isEditing.value){
        currentNote.value = {};
        noteHtmlRef.value.innerHTML = '';
    }
}

onMounted(() => {
    // 获取便签数据
    getNotes();
})
</script>
<style lang="less" scoped>
.wraps {
    height: 100%;
    width: 100%;
    padding: 8px;
    position: relative;
    .editors {
        display: flex;
        flex-direction: column;
        position: absolute;
        z-index: 99;
        left: 100%;
        top: 0;
        width: 100%;
        height: 100%;
        background-color: #fff;
        opacity: 0;
        transition: all .2s ease-in-out 0s;
        -webkit-app-region: no-drag;
        padding: 10px 20px;
        box-sizing: border-box;
        &-warp {
            -webkit-app-region: no-drag;
            font-size: 14px;
            width: 100%;
            margin-top: 10px;
            box-sizing: border-box;
            flex: 1;
            &>div {
                width: 100%;
                height: 100%;
                font-size: 16px;
                overflow: hidden;
                outline-style: none;
            }
        }
        .toolbars {
            display: flex;
        }
        &-top {
            margin-bottom: 16px;
            display: flex;
            align-items: center;
            .back{
                margin-right: 16px;
                -webkit-app-region: no-drag;
                cursor: pointer;
            }
            .title{
                font-size: 18px;
                font-weight: bold;
            }
            .saves {
                font-weight: bold;
                padding: 3px 6px;
                color: #475569;
                margin-left: auto;
                -webkit-app-region: no-drag;
                cursor: pointer;
                transition: all .1s ease-in 0s;
                &:hover {
                    color: #1C79F4;
                }
            }
        }
        
        &.isshow {
            opacity: 1;
            left: 0;
        }
        .errors {
            font-size: 12px;
            color: #f43f5e;
            height: 60px;
        }
        .default {
            font-size: 12px;
            color: #657685;
            height: 60px;
        }
        .editwarp:empty::before{
            content: attr(data-hodle);
            font-size: .7rem;
            color: #64748B;
        }
    }
    .homewarp {
        position: relative;
        display: flex;
        flex-direction: column;
        height: 100%;
        transition: all .3s ease-in 0s;
        &.ishide {
            transform: scale(.9) translateX(100px);
        }
        .addbtns {
            position: absolute;
            bottom: 16px;
            right: 16px;
            display: flex;
            align-items: center;
            justify-content: center;
            width: 50px;
            height: 50px;
            border-radius: 50%;
            background: #fcc000;
            -webkit-app-region: no-drag;
            cursor: pointer;
            font-size: 22px;
            &:hover{
                background: #d4a827;
            }
        }
        .content-box{
            overflow: auto;
            .note-box{
                position: relative;
                display: flex;
                align-items: center;
                justify-content: space-between;
                height: 64px;
                margin: 8px 0;
                padding: 8px;
                background-color: #fff;
                border-radius: 10px;
                overflow: hidden;
                transition: all 0.2s;
                cursor: pointer;
                -webkit-app-region: no-drag;
                &:hover{
                    background: #FAEDC7;
                    & .delete-btn{
                        color: #213547;
                    }
                }
                .delete-btn{
                    transition: all 0.2s;
                    color: transparent;
                    &:hover{
                        color: #CC0000
                    }
                }
            }
        }
    }
}
</style>
